home *** CD-ROM | disk | FTP | other *** search
- Imports System.IO
-
- Public Class ListViewForm
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- Friend WithEvents ListView1 As System.Windows.Forms.ListView
- Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader
- Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader
- Friend WithEvents ColumnHeader3 As System.Windows.Forms.ColumnHeader
- Friend WithEvents ColumnHeader4 As System.Windows.Forms.ColumnHeader
- Friend WithEvents ColumnHeader5 As System.Windows.Forms.ColumnHeader
- Friend WithEvents cboSort As System.Windows.Forms.ComboBox
- Friend WithEvents btnSort As System.Windows.Forms.Button
- Friend WithEvents btnList As System.Windows.Forms.Button
- Friend WithEvents btnShowChecked As System.Windows.Forms.Button
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.Container
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.btnSort = New System.Windows.Forms.Button()
- Me.ListView1 = New System.Windows.Forms.ListView()
- Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader()
- Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader()
- Me.ColumnHeader3 = New System.Windows.Forms.ColumnHeader()
- Me.ColumnHeader4 = New System.Windows.Forms.ColumnHeader()
- Me.ColumnHeader5 = New System.Windows.Forms.ColumnHeader()
- Me.cboSort = New System.Windows.Forms.ComboBox()
- Me.btnList = New System.Windows.Forms.Button()
- Me.btnShowChecked = New System.Windows.Forms.Button()
- Me.SuspendLayout()
- '
- 'btnSort
- '
- Me.btnSort.Location = New System.Drawing.Point(272, 16)
- Me.btnSort.Name = "btnSort"
- Me.btnSort.Size = New System.Drawing.Size(72, 26)
- Me.btnSort.TabIndex = 3
- Me.btnSort.Text = "Sort"
- '
- 'ListView1
- '
- Me.ListView1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
- Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right)
- Me.ListView1.CheckBoxes = True
- Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3, Me.ColumnHeader4, Me.ColumnHeader5})
- Me.ListView1.Location = New System.Drawing.Point(8, 56)
- Me.ListView1.Name = "ListView1"
- Me.ListView1.Size = New System.Drawing.Size(664, 264)
- Me.ListView1.TabIndex = 0
- Me.ListView1.View = System.Windows.Forms.View.Details
- '
- 'ColumnHeader1
- '
- Me.ColumnHeader1.Text = "Name"
- Me.ColumnHeader1.Width = 100
- '
- 'ColumnHeader2
- '
- Me.ColumnHeader2.Text = "Size"
- Me.ColumnHeader2.Width = 75
- '
- 'ColumnHeader3
- '
- Me.ColumnHeader3.Text = "Date Created"
- Me.ColumnHeader3.Width = 139
- '
- 'ColumnHeader4
- '
- Me.ColumnHeader4.Text = "Date Modified"
- Me.ColumnHeader4.Width = 154
- '
- 'ColumnHeader5
- '
- Me.ColumnHeader5.Text = "Last Access"
- Me.ColumnHeader5.Width = 177
- '
- 'cboSort
- '
- Me.cboSort.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
- Me.cboSort.DropDownWidth = 144
- Me.cboSort.Items.AddRange(New Object() {"Name", "Size", "Date Created", "Date Modified", "Date Accessed"})
- Me.cboSort.Location = New System.Drawing.Point(112, 16)
- Me.cboSort.Name = "cboSort"
- Me.cboSort.Size = New System.Drawing.Size(144, 26)
- Me.cboSort.TabIndex = 2
- '
- 'btnList
- '
- Me.btnList.Location = New System.Drawing.Point(16, 16)
- Me.btnList.Name = "btnList"
- Me.btnList.Size = New System.Drawing.Size(72, 26)
- Me.btnList.TabIndex = 1
- Me.btnList.Text = "List files"
- '
- 'btnShowChecked
- '
- Me.btnShowChecked.Location = New System.Drawing.Point(360, 16)
- Me.btnShowChecked.Name = "btnShowChecked"
- Me.btnShowChecked.Size = New System.Drawing.Size(120, 26)
- Me.btnShowChecked.TabIndex = 3
- Me.btnShowChecked.Text = "Show Checked"
- '
- 'ListViewForm
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
- Me.ClientSize = New System.Drawing.Size(680, 325)
- Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnShowChecked, Me.btnSort, Me.cboSort, Me.btnList, Me.ListView1})
- Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.Name = "ListViewForm"
- Me.Text = "ListView Demo"
- Me.ResumeLayout(False)
-
- End Sub
-
- #End Region
-
- Private Sub btnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
- ' Get system directory
- Dim path As String = System.Environment.SystemDirectory
- ' show in caption
- Me.Text = "Files in " & path
-
- ' show file names in that directory, using alternating colors
- ShowFilesInListView(ListView1, System.Environment.SystemDirectory)
- PaintAlternatingBackColor(ListView1, Color.White, Color.Cyan)
- ' select the first one
- cboSort.SelectedIndex = 0
- End Sub
-
- ' A reusable routine that shows files in specified directory
-
- Sub ShowFilesInListView(ByVal lv As ListView, ByVal path As String)
- ' Get the DirectoryInfo object corresponding to the path
- Dim di As New DirectoryInfo(path)
-
- lv.BeginUpdate()
- lv.Items.Clear()
-
- ' Get data about all the files.
- Dim fi As FileInfo
- For Each fi In di.GetFiles()
- ' Add a new ListViewItem object, add to the Items collection
- Dim item As New ListViewItem(fi.Name)
-
- lv.Items.Add(item)
-
- item.SubItems.Add(fi.Length)
- item.SubItems.Add(fi.CreationTime)
- item.SubItems.Add(fi.LastWriteTime)
- item.SubItems.Add(fi.LastAccessTime)
- Next
- lv.EndUpdate()
- End Sub
-
- ' A reusable routine that displays alternating color for row background
-
- Sub PaintAlternatingBackColor(ByVal lv As ListView, ByVal color1 As Color, ByVal color2 As Color)
- Dim item As ListViewItem
- Dim subitem As ListViewItem.ListViewSubItem
-
- For Each item In lv.Items
- ' set the color for the Item.
- If (item.Index Mod 2) = 0 Then
- item.BackColor = color1
- Else
- item.BackColor = color2
- End If
-
- ' Assign same color to all subitems.
- For Each subitem In item.SubItems
- subitem.BackColor = item.BackColor
- Next
- Next
- End Sub
-
- ' sorts the elements of a the ListView
-
- Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click
-
- ' Use the correct sort object.
- Dim index As Integer = cboSort.SelectedIndex
- Select Case index
- Case 0
- ' Sort by name.
- ListView1.ListViewItemSorter = New CompareByName()
- Case 1
- ' Sort by size.
- ListView1.ListViewItemSorter = New CompareBySize()
- Case 2, 3, 4
- ' Sort by one of the date properties.
- ListView1.ListViewItemSorter = New CompareByDate(index)
- End Select
-
- ' show we're busy doing the sort
- Cursor.Current = Cursors.WaitCursor
-
- ' Now we can sort the ListView.
- ListView1.Sort()
- PaintAlternatingBackColor(ListView1, Color.White, Color.Cyan)
- ' restore default cursor
- Cursor.Current = Cursors.Default
- End Sub
-
- ' an IComparer auxiliary class that helps to sort files by name
-
- Private Class CompareByName
- Implements IComparer
-
- Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
- ' cast the two arguments to ListViewItem objects
- Dim item1 As ListViewItem = DirectCast(x, ListViewItem)
- Dim item2 As ListViewItem = DirectCast(y, ListViewItem)
- ' compare their text property
- Return String.Compare(item1.Text, item2.Text)
- End Function
- End Class
-
- ' an IComparer auxiliary class that helps to sort files by their size
-
- Private Class CompareBySize
- Implements IComparer
-
- Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
- ' cast the two arguments to ListViewItem objects
- Dim item1 As ListViewItem = DirectCast(x, ListViewItem)
- Dim item2 As ListViewItem = DirectCast(y, ListViewItem)
- ' compare their 2nd subitem (the size)
- Return Math.Sign(CLng(item1.SubItems(1).Text) - CLng(item2.SubItems(1).Text))
- End Function
- End Class
-
- ' We can use one class for all date comparisons,
- ' just by passing a different subitem index.
-
- Private Class CompareByDate
- Implements IComparer
-
- Dim Index As Integer
-
- ' items in the ListView will be sorted on the date column whose
- ' index is passed to this constructor
-
- Sub New(ByVal subitemIndex As Integer)
- Me.Index = subitemIndex
- End Sub
-
- Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
- ' cast the two arguments to ListViewItem objects
- Dim item1 As ListViewItem = DirectCast(x, ListViewItem)
- Dim item2 As ListViewItem = DirectCast(y, ListViewItem)
- ' compare items on the specified datetime subitem
- Return Date.Compare(Date.Parse(item1.SubItems(Index).Text), Date.Parse(item2.SubItems(Index).Text))
- End Function
- End Class
-
- ' Display a list of all checked (selected) ListView items
-
- Private Sub btnShowChecked_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShowChecked.Click
- Dim msg As String = ""
- Dim item As ListViewItem
-
- For Each item In ListView1.CheckedItems
- msg &= item.Text & ControlChars.CrLf
- Next
-
- If msg.Length = 0 Then
- msg = "No items have been checked"
- Else
- msg = "List of checked items" & ControlChars.CrLf & ControlChars.CrLf & msg
- End If
- MessageBox.Show(msg, "CheckedItems collection")
- End Sub
- End Class
-